package com.vnp.vchat.database;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;

import com.telpoo.frame.utils.Mlog;
import com.vnp.vchat.bean.ObjKhachhang;
import com.vnp.vchat.bean.ObjMsgChat;

public class DBFunction {
	private DBAdapter db;

	public DBFunction(DBAdapter db) {
		this.db = db;
	}

	public void dropDB() {
		try {
			db.delete(MySQLiteHelper.TABLE_CONTACT_PHONE, "1=1");
			db.delete(MySQLiteHelper.TABLE_HISCHAT, "1=1");
			db.delete(MySQLiteHelper.TABLE_KH, "1=1");
			db.delete(MySQLiteHelper.TABLE_MESSAGE, "1=1");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// insert phan tu vao chat;
	public void insertMSG(ObjMsgChat objMsgChat, String relation, int status, String msgId,Context context){
		
		//.e("Tuan", "insert "+ objMsgChat.getContent() + " " + relation);
		
		if("6jdsf".equals(objMsgChat.getContent())){
		
			Log.i("err", "111");
		}
		
		try{
			if (!db.contains(MySQLiteHelper.TABLE_MESSAGE, MySQLiteHelper.MSG_ID, MySQLiteHelper.MSG_ID + "='"+msgId+"'")) {
				//Log.e("Tuan", "insert2 "+ objMsgChat.getContent());
				ContentValues value = new ContentValues();
				value.put(MySQLiteHelper.MSG_RELATION, relation);
				value.put(MySQLiteHelper.MSG_ID, msgId);
				value.put(MySQLiteHelper.MSG_SEND_ID, Integer.valueOf(objMsgChat.getFromId()));
				value.put(MySQLiteHelper.MSG_TIME, String.valueOf(objMsgChat.getTime()));
				value.put(MySQLiteHelper.MSG_TYPE, objMsgChat.getType());
				value.put(MySQLiteHelper.MSG_NAME, objMsgChat.getTenKhachHang());
				value.put(MySQLiteHelper.MSG_CONTENT, objMsgChat.getContent());
				value.put(MySQLiteHelper.MSG_URL_IMAGE, objMsgChat.getUrlImageKH());
				value.put(MySQLiteHelper.MSG_AVATAR_KH, objMsgChat.getUrlAvatarKhachhang());
				value.put(MySQLiteHelper.MSG_STATUS, String.valueOf(status));
				db.insert(MySQLiteHelper.TABLE_MESSAGE, value);
			//	Log.e("Tuan", "insert1 "+ objMsgChat.getContent());
			}
			
		} catch (Exception e) {
			//e.printStackTrace();
			//Mlog.E("DbFunction insertMSG error:"+objMsgChat.getContent());
		}
	}

	public void updateStatusMsg(String msgId, int status) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_STATUS, status);
		// update
		// db.update(TABLE_MESSAGE, values, MSG_ID + " = ?", new
		// String[]{msgId});
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_ID + " = " + msgId);
	}

	public void updateStatusMySMS(String msgId, int status, long vcg) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_STATUS, status);

		// update
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_ID + " = " + msgId);
	}

	public void updateMsg(String url, String key_row) {
		ContentValues values = new ContentValues();
		values.put(MySQLiteHelper.MSG_URL_IMAGE, url);

		// update
		db.update(MySQLiteHelper.TABLE_MESSAGE, values, MySQLiteHelper.MSG_SEND_ID + " = " + key_row);
	}

	public void deteleAllRecord(String key_row) {
		db.delete(MySQLiteHelper.TABLE_MESSAGE, MySQLiteHelper.MSG_RELATION + " = " + key_row);
	}

	public void deleteOneHisChat(String id) {
		db.delete(MySQLiteHelper.TABLE_HISCHAT, MySQLiteHelper.HS_UID + " = " + id);
	}

	public ArrayList<String> getIdHis() {
		ArrayList<String> items = new ArrayList<String>();
		String query = "SELECT " + MySQLiteHelper.HS_UID + " FROM " + MySQLiteHelper.TABLE_HISCHAT;
		String[] columns = { MySQLiteHelper.HS_UID };
		ArrayList<LinkedHashMap<String, Object>> records = db.fetch(MySQLiteHelper.TABLE_MESSAGE, columns, null, MySQLiteHelper.MSG_TIME + "DESC", 20, 0, null);
		for (int i = 0; i < records.size(); i++) {
			LinkedHashMap<String, Object> row = records.get(i);
			String id = (String) row.get(MySQLiteHelper.HS_UID);
			items.add(id);
		}
		return items;
	}

	// get all phan tu
	public ArrayList<ObjMsgChat> getAllMsgHis(String relation) {
		ArrayList<ObjMsgChat> objMsgChats = new ArrayList<ObjMsgChat>();
		String selectQuery = "SELECT * FROM " + MySQLiteHelper.TABLE_MESSAGE + " WHERE " + MySQLiteHelper.MSG_RELATION + "= " + relation;
		Log.i("SQL", selectQuery);

		String[] columns = { MySQLiteHelper.MSG_ID, MySQLiteHelper.MSG_SEND_ID, MySQLiteHelper.MSG_TIME, MySQLiteHelper.MSG_TYPE, MySQLiteHelper.MSG_NAME,
				MySQLiteHelper.MSG_CONTENT, MySQLiteHelper.MSG_URL_IMAGE, MySQLiteHelper.MSG_AVATAR_KH, MySQLiteHelper.MSG_STATUS };

		ArrayList<LinkedHashMap<String, Object>> records = db.fetch(MySQLiteHelper.TABLE_MESSAGE, columns, MySQLiteHelper.MSG_RELATION + "='" + relation + "'",
				MySQLiteHelper.MSG_TIME + " DESC", 100, 0, null);

		//Log.e("Tuan", "history = " + records.size());
		for (int i = 0; i < records.size(); i++) {

			ObjMsgChat objMsgChat = new ObjMsgChat();
			LinkedHashMap<String, Object> row = records.get(i);

			objMsgChat.setMsgID((String) row.get(MySQLiteHelper.MSG_ID));
			objMsgChat.setFromId((String) row.get(MySQLiteHelper.MSG_SEND_ID));

			try {
				objMsgChat.setTime(Integer.parseInt((String) row.get(MySQLiteHelper.MSG_TIME)));
			} catch (Exception e) {
			}

			objMsgChat.setType((String) row.get(MySQLiteHelper.MSG_TYPE));
			objMsgChat.setTenKhachHang((String) row.get(MySQLiteHelper.MSG_NAME));
			objMsgChat.setContent((String) row.get(MySQLiteHelper.MSG_CONTENT));
			objMsgChat.setUrlImageKH((String) row.get(MySQLiteHelper.MSG_URL_IMAGE));
			objMsgChat.setUrlAvatarKhachhang((String) row.get(MySQLiteHelper.MSG_AVATAR_KH));
			try {
				objMsgChat.setStatus(Integer.parseInt((String) row.get(MySQLiteHelper.MSG_STATUS)));
			} catch (Exception e) {
			}

			// add vao list
			objMsgChats.add(objMsgChat);
		}
		if (objMsgChats.size() == 100) {
			ObjMsgChat oj = objMsgChats.get(99);
			long time = oj.getTime();
			try {
				db.excuteSQL("delete * from " + MySQLiteHelper.TABLE_MESSAGE + "WHERE " + MySQLiteHelper.MSG_RELATION + "='" + relation + "'" + " AND " + MySQLiteHelper.MSG_TIME
						+ "<" + time + ";");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		ArrayList<ObjMsgChat> tmp = new ArrayList<ObjMsgChat>();
		for (int i = objMsgChats.size() - 1; i >= 0; i--) {
			tmp.add(objMsgChats.get(i));
		}
		return tmp;
	}

	public void insertContactSV(ObjKhachhang objKhachhang) {

		ContentValues values = new ContentValues();

		values.put(MySQLiteHelper.CT_ID_SV, objKhachhang.getId());
		values.put(MySQLiteHelper.CT_PHONE_ID_SV, objKhachhang.getPhone());
		values.put(MySQLiteHelper.CT_EMAIL_SV, objKhachhang.getEmail());
		values.put(MySQLiteHelper.CT_NAME_SV, objKhachhang.getName());
		values.put(MySQLiteHelper.CT_AVATAR_SV, objKhachhang.getUrlAvatar());
		values.put(MySQLiteHelper.CT_URL_SV, "");

		db.insert(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, values);

	}

	public void updateContactSV(ObjKhachhang objKhachhang) {

		ContentValues values = new ContentValues();

		values.put(MySQLiteHelper.CT_ID_SV, objKhachhang.getId());
		values.put(MySQLiteHelper.CT_PHONE_ID_SV, objKhachhang.getPhone());
		values.put(MySQLiteHelper.CT_EMAIL_SV, objKhachhang.getEmail());
		values.put(MySQLiteHelper.CT_NAME_SV, objKhachhang.getName());
		values.put(MySQLiteHelper.CT_AVATAR_SV, objKhachhang.getUrlAvatar());
		values.put(MySQLiteHelper.CT_URL_SV, "");

		boolean isExit = db.update(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, values, MySQLiteHelper.CT_PHONE_ID_SV + " = " + objKhachhang.getPhone());

		Log.i("SQL", "Update: " + isExit);

		if (isExit == false) {
			insertContactSV(objKhachhang);
			Log.i("SQL", "insert-------------------");
		} else {
			Log.i("SQL", "update-------------------");
		}
	}

	public ArrayList<ObjKhachhang> getContactSV() {
		ArrayList<ObjKhachhang> objKhachhangs = new ArrayList<ObjKhachhang>();
		String selectQuery = "SELECT * FROM " + MySQLiteHelper.TABLE_CONTACT_PHONE_SV;
		Log.i("SQL", selectQuery);

		String[] columns = { MySQLiteHelper.CT_PHONE_ID_SV, MySQLiteHelper.CT_EMAIL_SV, MySQLiteHelper.CT_NAME_SV, MySQLiteHelper.CT_AVATAR_SV };
		try {
			ArrayList<LinkedHashMap<String, Object>> records = db.fetchAll(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, null, null);

			Log.i("DATA", "RECORD SIZE: " + records.size());

			for (int i = 0; i < records.size(); i++) {

				ObjKhachhang objKhachhang = new ObjKhachhang();
				LinkedHashMap<String, Object> row = records.get(i);

				objKhachhang.setId((String) row.get(MySQLiteHelper.CT_ID_SV));
				objKhachhang.setPhone((String) row.get(MySQLiteHelper.CT_PHONE_ID_SV));
				objKhachhang.setEmail((String) row.get(MySQLiteHelper.CT_EMAIL_SV));
				objKhachhang.setName((String) row.get(MySQLiteHelper.CT_NAME_SV));
				objKhachhang.setUrlAvatar((String) row.get(MySQLiteHelper.CT_AVATAR_SV));
				objKhachhang.setUrlDangxem("");

				objKhachhangs.add(objKhachhang);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return objKhachhangs;
	}

	public void deleteContactSv() {
		db.delete(MySQLiteHelper.TABLE_CONTACT_PHONE_SV, "1=1");
	}
}
